home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / FRAME.C < prev    next >
C/C++ Source or Header  |  1980-01-03  |  10KB  |  353 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * A frame surrounds another interactor, providing borders, title banners, etc.
  25.  */
  26.  
  27. #include <InterViews/banner.h>
  28. #include <InterViews/border.h>
  29. #include <InterViews/box.h>
  30. #include <InterViews/canvas.h>
  31. #include <InterViews/frame.h>
  32. #include <InterViews/painter.h>
  33. #include <InterViews/pattern.h>
  34. #include <InterViews/shape.h>
  35. #include <InterViews/sensor.h>
  36.  
  37. Frame::Frame (Interactor* i, int w) {
  38.     Init(i, w, w, w, w);
  39. }
  40.  
  41. Frame::Frame (const char* name, Interactor* i, int w) {
  42.     SetInstance(name);
  43.     Init(i, w, w, w, w);
  44. }
  45.  
  46. Frame::Frame (Interactor* i, int l, int b, int r, int t) {
  47.     Init(i, l, b, r, t);
  48. }
  49.  
  50. Frame::Frame (const char* name, Interactor* i, int l, int b, int r, int t) {
  51.     SetInstance(name);
  52.     Init(i, l, b, r, t);
  53. }
  54.  
  55. void Frame::Init (Interactor* i, int l, int b, int r, int t) {
  56.     SetClassName("Frame");
  57.     left = l;
  58.     bottom = b;
  59.     right = r;
  60.     top = t;
  61.     if (i != nil) {
  62.     Insert(i);
  63.     }
  64. }
  65.  
  66. void Frame::Reconfig () {
  67.     MonoScene::Reconfig();
  68.     shape->width += left + right;
  69.     shape->height += bottom + top;
  70. }
  71.  
  72. void Frame::Resize () {
  73.     canvas->SetBackground(output->GetBgColor());
  74.     Place(component, left, bottom, xmax - right, ymax - top);
  75. }
  76.  
  77. void Frame::Redraw (Coord x1, Coord y1, Coord x2, Coord y2) {
  78. #ifndef _3D
  79.     register Coord r = xmax - right;
  80.     register Coord t = ymax - top;
  81.  
  82.     if (x1 < left) {
  83.         output->FillRect(canvas, 0, 0, left-1, t);
  84.     }
  85.     if (y1 < bottom) {
  86.         output->FillRect(canvas, left, 0, xmax, bottom-1);
  87.     } 
  88.     if (x2 > r) {
  89.         output->FillRect(canvas, r+1, bottom, xmax, ymax);
  90.     }
  91.     if (y2 > t) {
  92.         output->FillRect(canvas, 0, t+1, r, ymax);
  93.     }
  94. #endif
  95. #ifdef _3D
  96.     char flag=((x1<left) ? 8:0)+((y1<bottom) ? 4:0)+((x2>xmax-right) ? 2:0)+(y2>ymax-top);
  97.     output3D->Border(canvas,false,(left+right+top+bottom)/4,flag);
  98. #endif
  99. }
  100.  
  101. /*
  102.  * A ShowFrame highlights/unhighlights when it gets enter/leave events.
  103.  * These frames are generally only applicable to window manager or
  104.  * similar functionality; they are not recommended for general use.
  105.  */
  106.  
  107. void ShowFrame::Init () {
  108.     input = onoffEvents;
  109.     input->Reference();
  110. }
  111.  
  112. void ShowFrame::Handle (Event& e) {
  113.     if (e.eventType == EnterEvent) {
  114.         InsideFrame(true);
  115.     } else if (e.eventType == LeaveEvent) {
  116.         InsideFrame(false);
  117.     } else {
  118.     HandleInput(e);
  119.     }
  120. }
  121.  
  122. void ShowFrame::HandleInput (Event& e) {
  123.     component->Handle(e);
  124. }
  125.  
  126. void ShowFrame::InsideFrame (boolean) {
  127.     /* default is to do nothing */
  128. }
  129.  
  130. /*
  131.  * A title frame is a frame around a box containing
  132.  * a banner, border, and the component.
  133.  */
  134.  
  135. TitleFrame::TitleFrame (Banner* b, Interactor* i, int w) : (nil, w) {
  136.     Init(b, i);
  137. }
  138.  
  139. TitleFrame::TitleFrame (
  140.     const char* name, Banner* b, Interactor* i, int w
  141. ) : (name, nil, w) {
  142.     Init(b, i);
  143. }
  144.  
  145. void TitleFrame::Init (Banner* b, Interactor* i) {
  146.     SetClassName("TitleFrame");
  147.     banner = b;
  148.     if (i != nil) {
  149.     Insert(i);
  150.     }
  151. }
  152.  
  153. Interactor* TitleFrame::Wrap (Interactor* i) {
  154.     Scene* p = banner->Parent();
  155.     if (p != nil) {
  156.     p->Remove(banner);
  157.     }
  158.     return new VBox(banner, new HBorder, i);
  159. }
  160.  
  161. void TitleFrame::InsideFrame (boolean b) {
  162.     banner->highlight = b;
  163.     banner->Draw();
  164. }
  165.  
  166. /*
  167.  * A border frame draws an outline using a solid pattern when
  168.  * it contains the input focus and using a gray pattern otherwise.
  169.  */
  170.  
  171. BorderFrame::BorderFrame (Interactor* i, int w) : (i, w) {
  172.     Init();
  173. }
  174.  
  175. BorderFrame::BorderFrame (
  176.     const char* name, Interactor* i, int w
  177. ) : (name, i, w) {
  178.     Init();
  179. }
  180.  
  181. void BorderFrame::Init () {
  182.     SetClassName("BorderFrame");
  183.     normal = false;
  184. }
  185.  
  186. void BorderFrame::InsideFrame (boolean b) {
  187.     normal = b;
  188.     Redraw(0, 0, xmax, ymax);
  189. }
  190.  
  191. void BorderFrame::Redraw (Coord x1, Coord y1, Coord x2, Coord y2) {
  192.     if (normal) {
  193.     Frame::Redraw(x1, y1, x2, y2);
  194.     } else {
  195.     Pattern* save = output->GetPattern();
  196.     output->SetPattern(gray);
  197.     Frame::Redraw(x1, y1, x2, y2);
  198.     output->SetPattern(save);
  199.     }
  200. }
  201.  
  202. /*
  203.  * A shadow frame is a frame with a drop shadow.
  204.  */
  205.  
  206. ShadowFrame::ShadowFrame (Interactor* i, int h, int v) {
  207.     Init(i, h, v);
  208. }
  209.  
  210. ShadowFrame::ShadowFrame (
  211.     const char* name, Interactor* i, int h, int v
  212. ) : (name) {
  213.     Init(i, h, v);
  214. }
  215.  
  216. void ShadowFrame::Init (Interactor* i, int h, int v) {
  217.     if (h > 0) {
  218.     bottom += h;
  219.     } else {
  220.     top += -h;
  221.     }
  222.     if (v > 0) {
  223.     right += v;
  224.     } else {
  225.     left += -v;
  226.     }
  227.     if (i != nil) {
  228.     Insert(i);
  229.     }
  230. }
  231.  
  232. void ShadowFrame::Redraw (Coord x1, Coord y1, Coord x2, Coord y2) {
  233.     register Coord r = xmax - right;
  234.     register Coord t = ymax - top;
  235.     register Coord v = bottom + top - 2;
  236.     register Coord h = left + right - 2;
  237.  
  238.     /* borders */
  239.     if (x1 < left) {
  240.         output->FillRect(canvas, left-1, bottom-1, left-1, t);
  241.     }
  242.     if (y1 < bottom) {
  243.         output->FillRect(canvas, left, bottom-1, r+1, bottom-1);
  244.     }
  245.     if (x2 > r) {
  246.         output->FillRect(canvas, r+1, bottom, r+1, t+1);
  247.     }
  248.     if (y2 > t) {
  249.         output->FillRect(canvas, left-1, t+1, r, t+1);
  250.     }
  251.  
  252.     /* shadows */
  253.     if (left > 1 && x1 < left-1) {
  254.         output->FillRect(canvas, 0, v, left-2, ymax-v);
  255.     }
  256.     if (bottom > 1 && y1 < bottom-1) {
  257.         output->FillRect(canvas, h, 0, xmax-h, bottom-2);
  258.     }
  259.     if (right > 1 && x2 > r+1) {
  260.         output->FillRect(canvas, r+2, v, xmax, ymax-v);
  261.     }
  262.     if (top > 1 && y2 > t+1) {
  263.         output->FillRect(canvas, h, t+2, xmax-h, ymax);
  264.     }
  265.  
  266.     /* corner */
  267.     if (left > 1 && bottom > 1 && x1 < left-1 && y1 < bottom-1) {
  268.         output->FillRect(canvas, 0, 0, h - 1, v - 1);
  269.     } else if (left > 1 && top > 1 && x1 < left-1 && y2 > t+1) {
  270.         output->FillRect(canvas, 0, ymax - v + 1, h - 1, ymax);
  271.     } else if (right > 1 && bottom > 1 && x2 > r+1 && y1 < bottom-1) {
  272.         output->FillRect(canvas, xmax - h + 1, 0, xmax, v - 1);
  273.     } else if (right > 1 && top > 1 && x1 > r+1 && y2 > t+1) {
  274.         output->FillRect(canvas, xmax - h + 1, ymax - v + 1, xmax, ymax);
  275.     }
  276. }
  277.  
  278. /*
  279.  * A margin frame surrounds its component with horizontal and vertical
  280.  * glue.
  281.  */
  282.  
  283. MarginFrame::MarginFrame (Interactor* i, int margin) : (i, 0) {
  284.     Init(margin, 0, 0, margin, 0, 0);
  285. }
  286.  
  287. MarginFrame::MarginFrame (
  288.     const char* name, Interactor* i, int margin
  289. ) : (name, i, 0) {
  290.     Init(margin, 0, 0, margin, 0, 0);
  291. }
  292.  
  293. MarginFrame::MarginFrame (
  294.     Interactor* i, int margin, int shrink, int stretch
  295. ) : (i, 0) {
  296.     Init(margin, shrink, stretch, margin, shrink, stretch);
  297. }
  298.  
  299. MarginFrame::MarginFrame (Interactor* i, int hmargin, int vmargin) : (i, 0) {
  300.     Init(hmargin, 0, 0, vmargin, 0, 0);
  301. }
  302.  
  303. MarginFrame::MarginFrame (Interactor* i,
  304.     int hmargin, int hshrink, int hstretch,
  305.     int vmargin, int vshrink, int vstretch
  306. ) : (i, 0) {
  307.     Init(hmargin, hshrink, hstretch, vmargin, vshrink, vstretch);
  308. }
  309.  
  310. void MarginFrame::Init (int h, int hshr, int hstr, int v, int vshr, int vstr) {
  311.     SetClassName("MarginFrame");
  312.     hmargin = h * 2;
  313.     hshrink = hshr * 2;
  314.     hstretch = hstr * 2;
  315.     vmargin = v * 2;
  316.     vshrink = vshr * 2;
  317.     vstretch = vstr * 2;
  318. }
  319.  
  320. void MarginFrame::Reconfig () {
  321.     Frame::Reconfig();
  322.     shape->width += hmargin;
  323.     shape->height += vmargin;
  324.     shape->hshrink += hshrink;
  325.     shape->hstretch += hstretch;
  326.     shape->vshrink += vshrink;
  327.     shape->vstretch += vstretch;
  328. }
  329.  
  330. void MarginFrame::Resize () {
  331.     canvas->SetBackground(output->GetBgColor());
  332.  
  333.     Coord hextra = (xmax+1) - shape->width;
  334.     Coord h = hmargin;
  335.     if (hextra > 0 && shape->hstretch != 0) {
  336.         h += int(float(hstretch) / float(shape->hstretch) * float(hextra));
  337.     } else if (hextra < 0 && shape->hshrink != 0) {
  338.         h += int(float(hshrink) / float(shape->hshrink) * float(hextra));
  339.     }
  340.  
  341.     Coord vextra = (ymax+1) - shape->height;
  342.     Coord v = vmargin;
  343.     if (vextra > 0 && shape->vstretch != 0) {
  344.         v += int(float(vstretch) / float(shape->vstretch) * float(vextra));
  345.     } else if (vextra < 0 && shape->vshrink != 0) {
  346.         v += int(float(vshrink) / float(shape->vshrink) * float(vextra));
  347.     }
  348.  
  349.     Place(component, h/2, v/2, xmax-h/2, ymax-v/2);
  350. }
  351.  
  352. void MarginFrame::Redraw (Coord, Coord, Coord, Coord) { }
  353.